package com.mdc.mansion.web.modules.basedata.web;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

public class QrCodeTest {  
    
    /** 
     * 生成图像 
     *  
     * @throws WriterException 
     * @throws IOException 
     */  
    public void testEncode() throws WriterException, IOException {  
        String filePath = "D://";  
        String fileName = "zxing.png";  
        JSONObject json = new JSONObject();  
        json.put(  
                "zxing",  
                "www.baidu.com");  
        json.put("author", "shihy");  
        String content = json.toJSONString();// 内容  
        int width = 500; // 图像宽度  
        int height = 500; // 图像高度  
        String format = "png";// 图像类型  
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();  
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content,  
                BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵  
        File path = new File("E://aa.png");
        MatrixToImageWriter.writeToFile(bitMatrix, format, path);// 输出图像  
        System.out.println("输出成功.");  
    }  
    
   public static void main(String[] args) throws WriterException, IOException {
       QrCodeTest test = new QrCodeTest();
       test.testEncode();
   }
   
} 